package com.example.luke.shengdangxiaoattendance.http;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 01
 *
 * @time 2016/10/19. 18:22.
 * @des 网络请求的url
 */
public class NetWorkConstant {

    public static String BASE_URL = "http://61.140.16.238:63000/APPInterface/appService?";



    public static final String HTTP_Login="6001";

    public static final String HTTP_changePass="6002";
    public static final String HTTP_areaBalanceList="6003";

    public static final String HTTP_areaDetailBalanceList="6005";


    public static final String HTTP_chargeBalance="6004";

    public static final String HTTP_chargeFlowList="6005";


    public static final String HTTP_businessFlowList="6006";

    public static final String HTTP_detailReportList="6007";


    public  static String defaultIp="61.140.16.238";
    public  static String defaultPort="63000";

    public static  void initNetwork(Context context){

        SharedPreferences preferences=context.getSharedPreferences("network", Context.MODE_PRIVATE);
        defaultIp=preferences.getString("ip",defaultIp);
        defaultPort=preferences.getString("port",defaultPort);

        reformatBaseURL();



    }
    private static void reformatBaseURL(){

        NetWorkConstant.BASE_URL="http://"+defaultIp+":"+defaultPort+"/APPInterface/appService?";


    }

    public static  void saveNetwork(Context context, String ip, String port){

        SharedPreferences preferences=context.getSharedPreferences("network", Context.MODE_PRIVATE);
        defaultIp=ip;
        defaultPort=port;

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("ip",ip);
        editor.putString("port",port);
        editor.apply();

        reformatBaseURL();


    }


    public static boolean isIPAddress(String ipaddr) {
        boolean flag = false;
        Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
        Matcher m = pattern.matcher(ipaddr);
        flag = m.matches();
        return flag;

    }


}
